/// ===========================================================================
/// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
/// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
/// PURPOSE.
/// ===========================================================================
/// 
/// Project:        SharePoint Faceted Search
/// Author:         Leonid Lyublinski (leonidly@microsoft.com)
/// Company:        Microsoft Services
/// Date:           05/04/2007  Version:        1.0
///
/// ===========================================================================
/// $History: $
/// 
/// ===========================================================================

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Xml.Serialization;
using System.Data;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search.Query;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Microsoft.SharePoint.Portal.ExtendedSearch.WebControls
{
    /// <summary>
    /// Custom search bread crumbs web part.
    /// The web part bound to the querystring in and out.
    /// </summary>
    [ToolboxData("<{0}:SearchBreadCrumbs runat=server></{0}:SearchBreadCrumbs>")]
    [XmlRoot(Namespace = "Microsoft.SharePoint.Portal.ExtendedSearch")]
    public class SearchBreadCrumbs : System.Web.UI.WebControls.WebParts.WebPart
    {
        #region Constants
        
        #endregion

        #region Fields
        private string _scope;
        private string _k;
        private int _start;
        private string _v;
        private NameValueCollection _properties;

        private string _headerCss; // = "ms-bold";
        private string _facetCss; // = "ms-bold";
        private string _separator; // = " + ";
        private string _facetContainerCss; // = "ms-quicklaunch";
        private string _containerCss; // = "ms-quicklaunch";
        private int _repeatColumns;
        #endregion

        #region Properties
         /// <summary>
        /// The CSS class for the face datalist.
        /// </summary>
        [WebBrowsable(true)]
        [Category("Results Display")]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [Personalizable(PersonalizationScope.Shared)]
        [FriendlyName("Repeat Columns")]
        [Description("Number of table columns.")]
        public int RepeatColumns
        {
            get
            {
                return _repeatColumns;
            }
            set
            {
                _repeatColumns = value;
            }
        }

        /// <summary>
        /// The CSS class for the face datalist.
        /// </summary>
        [WebBrowsable(true)]
        [Category("Results Display")]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [Personalizable(PersonalizationScope.Shared)]
        [FriendlyName("Container CSS Class")]
        [Description("Select CSS class for the entire container.")]
        public string ContainerCss
        {
            get
            {
                return _containerCss;
            }
            set
            {
                _containerCss = value;
            }
        }

        /// <summary>
        /// The CSS class for the face value.
        /// </summary>
        [WebBrowsable(true)]
        [Category("Results Display")]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [Personalizable(PersonalizationScope.Shared)]
        [FriendlyName("Keyword CSS Class")]
        [Description("Select CSS class for the keyword.")]
        public string HeaderCss
        {
            get
            {
                return _headerCss;
            }
            set
            {
                _headerCss = value;
            }
        }
 
        /// <summary>
        /// The CSS class for the face datalist.
        /// </summary>
        [WebBrowsable(true)]
        [Category("Results Display")]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [Personalizable(PersonalizationScope.Shared)]
        [FriendlyName("Facet Container CSS Class")]
        [Description("Select CSS class for the facet container.")]
        public string FacetContainerCss
        {
            get
            {
                return _facetContainerCss;
            }
            set
            {
                _facetContainerCss = value;
            }
        }

        /// <summary>
        /// The CSS class for the face name.
        /// </summary>
        [WebBrowsable(true)]
        [Category("Results Display")]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [Personalizable(PersonalizationScope.Shared)]
        [FriendlyName("Facet CSS Class")]
        [Description("Select CSS class for the facet header.")]
        public string FacetCss
        {
            get
            {
                return _facetCss;
            }
            set
            {
                _facetCss = value;
            }
        }

     
        /// <summary>
        /// A text used as a separator between bread crumbs.
        /// </summary>
        [WebBrowsable(true)]
        [Category("Results Display")]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [Personalizable(PersonalizationScope.Shared)]
        [FriendlyName("Bread Separator")]
        [Description("Will be used to separate bread crumbs.")]
        public string Separator
        {
            get
            {
                return _separator;
            }
            set
            {
                _separator = value;
            }
        }


        #endregion

        #region Delegates
        /// <summary>
        /// Constructs the Controls collection of the webpart. 
        /// </summary _
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            try
            {
                if (Context.Request.QueryString != null)
                {
                    SearchQuery query = Utility.ExtractQuery();
                    _k = query.Keywords;
                    _scope = query.Scope;
                    _start = query.Start;
                    _v = query.View;
                    _properties = query.Properties;
                
                    ListDictionary dict = new ListDictionary();
                    
                    foreach (string prop in _properties) dict.Add(prop, _properties[prop]);
                    Label lSearch = new Label();
                    lSearch.EnableViewState = false;
                    lSearch.CssClass = _headerCss;
                    lSearch.Text = (string.IsNullOrEmpty(_k))?"":"Search for:" + _k;
                                                        
                    DataList list = new DataList();
                    list.CssClass = _facetContainerCss;
                    list.CellPadding = 2;
                    list.CellSpacing = 0;
                    list.RepeatLayout = RepeatLayout.Table;
                    //list.RepeatDirection = RepeatDirection.Horizontal;
                    list.RepeatColumns = _repeatColumns;
                    //list.Attributes.Add("style", "display:inline");
                    list.ItemTemplate = new CrumbTemplate(ListItemType.Item, _facetCss);
                    //list.HeaderTemplate = new HeaderTemplate(ListItemType.Header, _headerCss, header);
                    list.SeparatorTemplate = new SeparatorTemplate(ListItemType.Separator, _facetCss, _separator);
                    list.DataSource = dict;                
                    list.DataBind();

                    HtmlTable table = BreadCrumbsTable(lSearch, list);

                    Controls.Add(table);
                }
            }
            catch (Exception ex)
            {
                // remove label when done troubleshooting
                Label error = new Label();
                error.Text = ex.Message;
                Controls.Add(error);
            }
        }
        #endregion

        #region Private Methods
        private HtmlTable BreadCrumbsTable(Control keyword, Control metas)
        {
            HtmlTable table = new HtmlTable();
            table.Attributes.Add("class", _containerCss);
            //table.Align = "right";
            table.CellPadding = 2;
            table.CellSpacing = 0;
            table.Border = 0;

            HtmlTableRow rowKeyword = new HtmlTableRow();            
            HtmlTableCell cellKeyword = new HtmlTableCell();
            cellKeyword.Controls.Add(keyword);
            cellKeyword.NoWrap = true;
            rowKeyword.Cells.Add(cellKeyword);            
            table.Rows.Add(rowKeyword);

            HtmlTableRow rowMetas = new HtmlTableRow();
            HtmlTableCell cellMetas = new HtmlTableCell();
            cellMetas.NoWrap = true;
            cellMetas.Controls.Add(metas);
            rowMetas.Cells.Add(cellMetas);
            table.Rows.Add(rowMetas);

            return table;
        }
        #endregion
    }
}
